package com.graduation.check.controller;

import com.graduation.check.dto.TeacherStatisticsResponse;
import com.graduation.check.service.TeacherStatisticsService;
import com.graduation.check.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/teacher/statistics")  // context-path=/api已在application.properties配置
@CrossOrigin(origins = "*")
public class TeacherStatisticsController {
    
    @Autowired
    private TeacherStatisticsService teacherStatisticsService;
    
    /**
     * 获取综合统计数据
     */
    @GetMapping("/overview")
    public ApiResponse<TeacherStatisticsResponse> getOverviewStatistics(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
            @RequestParam(required = false) String major,
            @RequestParam(required = false) String className) {
        try {
            System.out.println("收到获取综合统计数据请求");
            TeacherStatisticsResponse response = teacherStatisticsService.getOverviewStatistics(
                startDate, endDate, major, className);
            System.out.println("综合统计数据获取成功: " + response);
            return ApiResponse.success(response);
        } catch (Exception e) {
            System.err.println("获取统计数据失败: " + e.getMessage());
            e.printStackTrace();
            return ApiResponse.error(500, "获取统计数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取趋势分析数据
     */
    @GetMapping("/trends")
    public ApiResponse<Map<String, Object>> getTrendStatistics(
            @RequestParam(defaultValue = "day") String period,
            @RequestParam(defaultValue = "30") Integer days,
            @RequestParam(required = false) String groupBy) {
        try {
            System.out.println("收到获取趋势分析数据请求");
            Map<String, Object> result = teacherStatisticsService.getTrendStatistics(period, days, groupBy);
            System.out.println("趋势分析数据获取成功");
            return ApiResponse.success(result);
        } catch (Exception e) {
            System.err.println("获取趋势数据失败: " + e.getMessage());
            e.printStackTrace();
            return ApiResponse.error(500, "获取趋势数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取学生活跃度分析数据
     */
    @GetMapping("/activity")
    public ApiResponse<Map<String, Object>> getStudentActivity() {
        try {
            System.out.println("收到获取学生活跃度分析数据请求");
            Map<String, Object> result = teacherStatisticsService.getStudentActivity();
            System.out.println("学生活跃度分析数据获取成功");
            return ApiResponse.success(result);
        } catch (Exception e) {
            System.err.println("获取活跃度数据失败: " + e.getMessage());
            e.printStackTrace();
            return ApiResponse.error(500, "获取活跃度数据失败: " + e.getMessage());
        }
    }
}